package com.mengfou.tasks;

import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.TaskAction;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author 梦否 on 2023/1/14
 * @blog https://mengfou.blog.csdn.net/
 */
public class AnnotationTask extends DefaultTask {

    @TaskAction
    void handle(){
        File singleFile = getOutputs().getFiles().getSingleFile();
        singleFile.mkdirs();

        for (File file : getInputs().getFiles()) {
            if(file != null && file.getName().endsWith("java")) {
                // 这里不解析了
                File temp = new File(singleFile, "MainActivity_log.java");
                String content = "import androidx.appcompat.app.AppCompatActivity;\n" +
                        "import android.os.Bundle;\n" +
                        "import android.util.Log;\n" +
                        "public class MainActivity_log extends AppCompatActivity {\n" +
                        "\n" +
                        "    @Override\n" +
                        "    protected void onCreate(Bundle savedInstanceState) {\n" +
                        "        super.onCreate(savedInstanceState);\n" +
                        "        Log.e(\"TAG\", \"onCreate: Hello!\");\n" +
                        "    }\n" +
                        "}";
                FileOutputStream fileOutputStream = null;
                try {
                    fileOutputStream = new FileOutputStream(temp);
                    fileOutputStream.write(content.getBytes());
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if(fileOutputStream != null) {
                        try {
                            fileOutputStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}
